<!--
 * @Author: wangding
 * @Date: 2020-11-11 15:58:31
 * @LastEditTime: 2021-01-25 18:57:06
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\device\sale\add.vue
-->
<template>
    <div class="buyPage-main-box-add">
        <el-dialog
        :title="$t('add')"
        :visible="newVisible"
        width="40%"
        @close="()=>{$store.commit('setNewVisible',false)}"
        :close-on-click-modal='false'
        >
            <el-row :gutter="20">
                <el-col :span="18" :offset="2">
                    <el-form ref="form" :model="form" label-width="200px" >
                        <el-form-item :label="$t('devWifi.wifiSN')" prop="wifiSN" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-input v-model="form.wifiSN" :placeholder="$t('pleaseInput')"></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('servicechange.defaultServcie')" prop="defaultService" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-input v-model="form.defaultService" :placeholder="$t('pleaseInput')" :disabled="true"></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('servicechange.targetServcie')" prop="targetService" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-select v-model="form.targetService" :placeholder="$t('pleaseSelect')" clearable style="width:100%;">
                                <el-option
                                v-for="item in serviceList"
                                :key="item.outsideHost+':'+item.outsidePort"
                                :label="item.outsideHost+':'+item.outsidePort"
                                :value="item.outsideHost+':'+item.outsidePort">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setNewVisible',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')">{{$t('sure')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    components: {

    },
    props: {
        
    },
    data() {
        return {
            form:{
                wifiSN:"",
                defaultService:"",
                targetService:"",
                mqttConfigId:"",
            },
            flag:false,
            serviceList:[]
        };
    },
    computed: {
        newVisible(){
            return this.$store.state.sysStore.newVisible
        }
    },
    watch: {
        serviceList:{
            handler(nv,ov){
                if(nv&&nv.length>0){
                    nv.forEach(item =>{
                        if(item.defaultFlag == 1){
                            this.form.mqttConfigId = item.id
                            this.form.defaultService = item.outsideHost+':'+item.outsidePort
                        }
                    })
                }
            }
        }
    },
    created() {

    },
    mounted() {
        this.mqttConfigGetMqttList()
    },
    methods: {
        async mqttConfigGetMqttList(){
            let res = await this.API.mqttConfigGetMqttList()
            if(res!=null && res.length>0){
                this.serviceList = res
            }
        },
        async wifiVidateWifiSNExist(){
            let res=  await this.API.wifiVidateWifiSNExist({
                wifiSN : this.form.wifiSN
            })
            if(res){
                this.flag = true
                this.mqttConfigUpdateMqttConfig()
            }else{
                this.flag = false
                this.comm.showMsg(false,this.$t('transferRates.js.wifisn.error'))
            }
        },
        async mqttConfigUpdateMqttConfig(){
            let webTime = new Date()
            console.log(webTime,'webTime');
            let res = await this.API.mqttConfigUpdateMqttConfig({
                wifiSN:this.form.wifiSN,
                mqttConfigId:this.form.mqttConfigId,
                targetService:this.form.targetService,
                webTime:webTime
            })
            if(this.comm.showMsg(res.success)){
                this.$store.commit('setNewVisible',false)
                this.$emit('uploadData',true)
            }
        },
        save(formName){
            this.$refs[formName].validate((valid) => {
              if (valid) {
                  this.wifiVidateWifiSNExist()
              } else {
                console.log('error submit!!');
                return false;
              }
            });
        },
    },
};
</script>

<style  lang="less" >
.buyPage-main-box-add{
    .el-form-item__content{
        display: flex;
    }
    .icon-fontsize{
        font-size: 25px;
        margin-left: 10px;
        display: block;
        width: 25px;
        height: 33px;
    }
}
</style>
